package com.zhkj.shixi.tvplayer.VideoPlayer;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.zhkj.shixi.tvplayer.MyMediaPlayer;
import com.zhkj.shixi.tvplayer.R;

import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by suqiuyu on 19-8-7.
 */

public class VideoPlayerUI implements View.OnClickListener, View.OnKeyListener {
    private Activity mActivity;
    private ImageButton btnPrev;
    private ImageButton btnPlay;
    private ImageButton btnNext;
    private ImageButton btnRewind;
    private ImageButton btnSkip;
    private ImageView imgPrev;
    private ImageView imgNext;
    //播放顺序
    private Button mSingle;
    private Button mOrder;
    private Button mCycle;
    private Button mRand;

    private View ctrlLayout;
    private View menuLayout;
    private VideoPlayerMenu mMenu;
    private Timer mTimer;
    private SeekBar seekBar;
    private View settingLayout;
    private ListView listView;
    private TextView textTitle;
    private View listlayout;
    private boolean isStartMove = false;
    public VideoPlayerUI(Activity activity) {
        mActivity = activity;
        mTimer = new Timer();
        initUI();
        mMenu = new VideoPlayerMenu(activity);
        autoHibitCtrlBoard();
    }

    private void initUI() {
        btnNext = mActivity.findViewById(R.id.btnNext);
        btnPrev = mActivity.findViewById(R.id.btnPrev);
        btnPlay = mActivity.findViewById(R.id.btnPlay);
        btnRewind = mActivity.findViewById(R.id.btnRewind);
        btnSkip = mActivity.findViewById(R.id.btnSkip);
        imgNext = mActivity.findViewById(R.id.imgNext);
        imgPrev = mActivity.findViewById(R.id.imgPrev);
        menuLayout = mActivity.findViewById(R.id.menu);
//        menuLayout = mActivity.findViewById(R.id.menu_layout);
        ctrlLayout = mActivity.findViewById(R.id.ctrl);
        seekBar = mActivity.findViewById(R.id.seekBar);
        settingLayout = mActivity.findViewById(R.id.setting);
        listView =mActivity.findViewById(R.id.videoList);
        textTitle = mActivity.findViewById(R.id.VideoTitle);
        listlayout =mActivity.findViewById(R.id.list_show);

        btnNext.setOnClickListener(this);
        btnPrev.setOnClickListener(this);
        btnPlay.setOnClickListener(this);
        btnRewind.setOnClickListener(this);
        btnSkip.setOnClickListener(this);
        btnRewind.setOnKeyListener(this);
        btnSkip.setOnKeyListener(this);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//                    Log.i("seekBar", "onProgressChanged: ");
                if (fromUser) {
                    autoHibitCtrlBoard();
                    ((VideoPlayerActivity) mActivity).getPlayCtrl().refreshByProcessBar(
                            ((VideoPlayerActivity) mActivity).getMyMediaPlayer(),seekBar.getProgress()*1000);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        BottomControlBar playCtrl = ((VideoPlayerActivity) mActivity).getPlayCtrl();
        VideoPlayOrder videoPlayOrder =((VideoPlayerActivity) mActivity).getmPlayOrder();
        MyMediaPlayer player = ((VideoPlayerActivity) mActivity).getMyMediaPlayer();
        PlayerView playerView = ((VideoPlayerActivity) mActivity).getPlayerView();
        VideoFull videoFull = ((VideoPlayerActivity) mActivity).getVideoFull();
        switch (v.getId()) {
            case R.id.btnPlay:
                //播放
                boolean isPlaying = player.isPlaying();
                if (isPlaying) {
                    playCtrl.pause(player);
                } else {
                    playCtrl.play(player);
                }
                isPlaying = player.isPlaying();
                changePlayButton(isPlaying);
                break;
            case R.id.btnSkip:
                //快进
                playCtrl.fastGo(player);
                break;
            case R.id.btnRewind:
                playCtrl.fastBack(player);
                //快退
                break;
            case R.id.btnNext:
                playCtrl.next(player,playerView.getHolder());
                upgradeUI();
                videoFull.setFull();
                //下一个视频
                break;
            case R.id.btnPrev:
                playCtrl.prev(player,playerView.getHolder());
                upgradeUI();
                videoFull.setFull();
                //上一个视频
                break;
        }
        autoHibitCtrlBoard();
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        BottomControlBar playCtrl = ((VideoPlayerActivity) mActivity).getPlayCtrl();
        MyMediaPlayer player = ((VideoPlayerActivity) mActivity).getMyMediaPlayer();
        if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                switch (v.getId()) {
                    case R.id.btnSkip:
                        playCtrl.fastGo(player);
                        autoHibitCtrlBoard();
                        return true;
                    case R.id.btnRewind:
                        playCtrl.fastBack(player);
                        autoHibitCtrlBoard();
                        return true;
                }
            }
        }
        autoHibitCtrlBoard();
        return false;
    }

    /**
     * 获取屏幕宽度
     * @author linjing
     * @return 屏幕宽度
     */
    private int getWidth() {
        // 获取屏幕的长宽像素
        DisplayMetrics dm = new DisplayMetrics();
        // 此处this为一个activity
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    /**
     * 菜单侧滑效果
     * @author linjing
     */
    public void slideMenuVisiblity() {
        if (View.VISIBLE == menuLayout.getVisibility()) {
            ObjectAnimator oa = ObjectAnimator.ofFloat(menuLayout, "translationX", 0, getWidth() / 100 * 8);
            oa.setDuration(500);
            oa.start();
            menuLayout.setVisibility(View.INVISIBLE);
        } else if (!isMenuVisiableWithoutCtrl()) {
            ObjectAnimator oa = ObjectAnimator.ofFloat(menuLayout, "translationX", getWidth() / 100 * 8, 0);
            oa.setDuration(500);
            oa.start();
            menuLayout.setVisibility(View.VISIBLE);
            hideCtrlLayout();
            //reqeust focous
            ImageButton button = mActivity.findViewById(R.id.btnList);
            button.requestFocus();
        }
    }

    public void changeMenuVisiblity() {
        if (View.VISIBLE == menuLayout.getVisibility()) {
            menuLayout.setVisibility(View.INVISIBLE);
        } else if (View.INVISIBLE == menuLayout.getVisibility()) {
            menuLayout.setVisibility(View.VISIBLE);
            //reqeust focous
            ImageButton button = mActivity.findViewById(R.id.btnList);
            button.requestFocus();
        }
    }

    public void autoHibitCtrlBoard() {
        mTimer.cancel();
        mTimer = new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
//                        ctrlLayout.setVisibility(View.INVISIBLE);
                        hideCtrlLayout();
                    }
                });
            }
        },5000);
    }
    public void displayCtrlBoard() {
//        ctrlLayout.setVisibility(View.VISIBLE);
        displayCtrlLayout();
        autoHibitCtrlBoard();
    }

    public void changePlayButton(boolean isPlaying) {
        if (isPlaying) {
            btnPlay.setImageDrawable(mActivity.getResources().getDrawable(R.drawable.video_btn_suspend));
        } else {
            btnPlay.setImageDrawable(mActivity.getResources().getDrawable(R.drawable.video_btn_play));
        }
    }
    public boolean isMenuVisiable() {
        boolean b =
//                (ctrlLayout.getVisibility() != View.INVISIBLE) ||
                isCtrlLayoutVisible()||
                (menuLayout.getVisibility() != View.INVISIBLE) ||
                (settingLayout.getVisibility() != View.INVISIBLE) ||
                (listlayout.getVisibility() != View.INVISIBLE);
        return b;
    }

    public boolean isMenuVisiableWithoutCtrl() {
        boolean b =
                (menuLayout.getVisibility() != View.INVISIBLE) ||
                (settingLayout.getVisibility() != View.INVISIBLE) ||
                (listlayout.getVisibility() != View.INVISIBLE);
        return b;
    }

    public ListView getListView() {
        return listView;
    }

    public void upgradeUI() {
        Log.i("upgradeUI", ((VideoPlayerActivity) mActivity).getPlayingFile().getName());
        MyMediaPlayer player = ((VideoPlayerActivity) mActivity).getMyMediaPlayer();
        changePlayButton(player.isPlaying());
        upgradeSeekBar(player);
        ((VideoPlayerActivity) mActivity).getPlayCtrl().miniMap(mActivity);
        textTitle.setText(((VideoPlayerActivity) mActivity).getPlayingFile().getName());
    }

    private Timer mSeekBarTimer = new Timer();
    public void upgradeSeekBar(final MyMediaPlayer player) {
        seekBar.setMax(player.getDuration() / 1000);
        TextView textDuration = mActivity.findViewById(R.id.textDuration);
        final SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");
        textDuration.setText(formatter.format(player.getDuration()));
        mSeekBarTimer.cancel();
        mSeekBarTimer = new Timer();
        mSeekBarTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        seekBar.setProgress(player.getCurrentPosition() / 1000);
                        TextView textCurTime = mActivity.findViewById(R.id.textCurTime);
                        textCurTime.setText(formatter.format(player.getCurrentPosition()));
                    }
                });
            }
        },0,300);
    }

    public View getListlayout() {
        return listlayout;
    }

    public void hideCtrlLayout() {
        View VideoTitle = ctrlLayout.findViewById(R.id.VideoTitle);
        View bottom_layout = ctrlLayout.findViewById(R.id.bottom_layout);
        View textPlayTime = ctrlLayout.findViewById(R.id.textPlayTime);
        VideoTitle.setVisibility(View.INVISIBLE);
        bottom_layout.setVisibility(View.GONE);
        textPlayTime.setVisibility(View.INVISIBLE);
    }

    public void displayCtrlLayout() {
        View VideoTitle = ctrlLayout.findViewById(R.id.VideoTitle);
        View bottom_layout = ctrlLayout.findViewById(R.id.bottom_layout);
        View textPlayTime = ctrlLayout.findViewById(R.id.textPlayTime);
        VideoTitle.setVisibility(View.VISIBLE);
        bottom_layout.setVisibility(View.VISIBLE);
        textPlayTime.setVisibility(View.VISIBLE);
    }

    public boolean isCtrlLayoutVisible() {
        View VideoTitle = ctrlLayout.findViewById(R.id.VideoTitle);
        if (VideoTitle.getVisibility() == View.VISIBLE) {
            return true;
        } else {
            return false;
        }
    }
}
